package com.example.finalapp;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.example.finalapp.ConfirmCancelDialog.OnSelectedListener;
import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseQuery;
import com.parse.ParseUser;

public class PushApartmentAdminActivity extends Activity implements OnSelectedListener{
	String name_roommate = "Jhon Doe";
	String email_roommate = "Jhon@Doe.Doe";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_push_apartment_admin);

		Parse.initialize(this, "6xDnXjWOcfFRJMlrIJxdVnvtjlTqRgtq8VnSh9Qn", "cxSxdJr5nke0qSxPAIYgU8TwLmzsXZnyzM5U4z8t");

		Intent intent = getIntent(); 
		Bundle extras = intent.getExtras(); 
		String jsonDataStr = extras.getString("com.parse.Data");

		try {
			JSONObject jsonData= new JSONObject(jsonDataStr);
			name_roommate= jsonData.getString("Name");
			email_roommate= jsonData.getString("Email");
		} 
		catch (JSONException e) {
			e.printStackTrace();
		}

		ConfirmCancelDialog new_dialog= new ConfirmCancelDialog();
		Bundle args = new Bundle();

		args.putString(ConfirmCancelDialog.ARG_STRING_ID, 
				name_roommate + ", " + email_roommate + ", "+ getResources().getString(R.string.admin_confirmation));
		args.putBoolean(ConfirmCancelDialog.ARG_FLAG, true);
		new_dialog.setArguments(args);
		new_dialog.show(getFragmentManager(), "newDialog");
	}

	@Override
	public void onCancelSelected(boolean isAdminDialog) {
		ParseQuery<ParseUser> query = ParseUser.getQuery();
		query.whereEqualTo("email", PushApartmentAdminActivity.this.email_roommate);
		query.whereEqualTo("Apartment", ParseUser.getCurrentUser().get("Apartment"));
		query.findInBackground(new FindCallback<ParseUser>() {
			public void done(List<ParseUser> objects, ParseException e) {
				if (e == null) 
				{
					objects.get(0).deleteInBackground();
					Intent intent = new Intent(PushApartmentAdminActivity.this, MainActivity.class);
					startActivity(intent);
				} 
				else 
				{
					// Something went wrong.
				}
			}
		});
	}

	@Override
	public void onConfirmSelected(boolean isAdminDialog) {
		ParseQuery<ParseUser> query = ParseUser.getQuery();
		query.whereEqualTo("email", PushApartmentAdminActivity.this.email_roommate);
		query.whereEqualTo("Apartment", ParseUser.getCurrentUser().get("Apartment"));
		query.findInBackground(new FindCallback<ParseUser>() {
			public void done(List<ParseUser> objects, ParseException e) {
				if (e == null) 
				{
					objects.get(0).put("isConfirmed", true);
					Intent intent = new Intent(PushApartmentAdminActivity.this, MainActivity.class);
					startActivity(intent);
				} 
				else 
				{
					// Something went wrong.
				}
			}
		});
	}

}
